;(function(){
  if (typeof $ == {undefined}){
    window.$ = window;
  }
})(window)

;(function ($) {


 $.TopRequest = function () {
    let ajax = new Z.Ajax();
    let setClassName = ajax.setClassName;
    let setMethodName = ajax.setMethodName;
    let setFailureAlert = ajax.setFailureAlert;
    let setFailure = ajax.setFailure;
    let setSuccess = ajax.setSuccess;
    let setLoading = ajax.setLoading;
    let addParam = ajax.addParam;
    let execute = ajax.execute;
    
    
    ajax.setClassName = function (call) {setClassName.call(ajax, call)};
    ajax.setMethodName = function (call) {
        setMethodName.call(ajax, call)
    };
    
    
    ajax.$complate = null;
    ajax.$failure = null;
    ajax.$failureAlertCall = null; //失败弹窗
    ajax.$failureAlert = false;
    
    ajax.$$complate = function (responseText) {
        if (Object.prototype.toString.call(ajax.$complate) == "[object Function]") {
          ajax.$complate(responseText);
        }
    };
      // 设置原有的方法
    ajax.setFailure(function (responseText) {
        ajax.$$complate(responseText);
        if (Object.prototype.toString.call(ajax.$failure) == "[object Function]") {
          ajax.$failure(responseText);
        }
    
        if (ajax.$failureAlert) {
          Z.failure(responseText, () => {
            if (Object.prototype.toString.call(ajax.$failureAlertCall) == "[object Function]") {
              ajax.$failureAlertCall(responseText);
            }
    
          });
        }
    })
    ajax.setFailureAlert = function (call) {
        ajax.$failureAlert = true;
        this.$failureAlertCall = call;
    };
    
    ajax.setFailure = function (call) {
        ajax.$failure = call;
    };
    
    ajax.setSuccess = function (call) {
        setSuccess.call(ajax, (responseText) => {
          this.$$complate(responseText);
          if (Object.prototype.toString.call(call) == "[object Function]") {
            call(responseText);
          }
        })
    };
    ajax.setComplete = function (call) {ajax.$complate = call};
    ajax.setLoading = function (param) {setLoading.call(ajax, param)};
    ajax.addParam = function (key, value) {addParam.call(ajax, key, value)};
    ajax.execute = function (call) {
        ajax.setSuccess(call);
        execute.call(ajax)
    };

    return ajax;
};

function type(obj){
    return Object.prototype.toString.call(obj).slice(8,-1)  
}
function isFunction(obj){
    return type(obj) === "Function"
}
  $.taobaoApi = {
    invoke: function (config) {
      let cfg = {...config};
      let param = cfg.param;
      
      let ajax = $.TopRequest();
      ajax.setClassName("GlobalPresenter");
      ajax.setMethodName("invokeTopApi");
      ajax.addParam("cmd", cfg.cmd || '');
      if(param !== undefined && param !== null){
        ajax.addParam("param",type(cfg.param) === "String"? cfg.param: JSON.stringify(param));
	  }
      ajax.setFailure(resp => {
        let err = {code: -100, msg: resp, sub_code: null, sub_msg: null};
        try {
          config = JSON.parse(resp);
          if (config.error_response) {
            err = config.error_response;
          }
        } catch (e) {
          console.error("$.taobaoApi.invoke=>failure ", e)
        }

        if (isFunction(cfg.error)) {
          cfg.error(err, cfg.cmd, cfg.param)
        }
      });
      ajax.execute((resp) => {
        try {
          resp = JSON.parse(resp);
        } catch (e) {
          console.error("$.taobaoApi.invoke=>success error", e);
          return;
        }

        if (isFunction(cfg.success)) {
          cfg.success(resp, cfg.cmd, cfg.param)
        }
      });
    }
  }

// 创建动态加载JS 函数
  let scriptLoader = {
    loadScript: function (src, callback) {
      // 加入小程序支持 onload 动态添加主要是为了解决页 卡顿问题
      let script = document.createElement("script")
      script.type = "text/javascript";
      if (script.readyState) {
        //IE
        script.onreadystatechange = function () {
          console.log(222)
          if (script.readyState == "loaded" || script.readyState == "complete") {
            script.onreadystatechange = null;
            console.log(111111)
            if (callback) {
              callback()
            }
          }
        };
      }
      else {
        //Others
        script.onload = function () {

        };
      }

      script.src = src + (src.indexOf("?") != -1 || src.indexOf("#") != -1 ? "&" : "?") + 't=' + new Date().getTime();
      return script;
    }
  }

/** 
// 载入小程序支持
  $(document).ready(function () {
    document.getElementsByTagName("head")[0].appendChild(scriptLoader.loadScript('https://appx/web-view.min.js', function(){
      if(typeof my == "undefined"){
        return;
      }
      my.onMessage = function(resp){
        console.log("my.onMessage", resp);
      }
    }));
  });
**/
})($);
    